package database;

// <editor-fold defaultstate="collapsed" desc="HeaderFiles">
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;// </editor-fold>

/**
 * @version
 *
 * @Date Mar 30, 2011
 * @author Rajeeb Saha
 *
 */
public class StoreDAO {

    // <editor-fold defaultstate="collapsed" desc="Private Variables">
    private Connection conn = null;
    private Statement sqlStatement;
    private static final Logger logger = Logger.getLogger(
            StoreDAO.class.getName());
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Constructor">
    /**
     *
     */
    public StoreDAO() {
        ConnectionDAO connectionDAO = new ConnectionDAO();
        conn = connectionDAO.getConn();
        sqlStatement = connectionDAO.getSqlStatement();
    }

    /**
     *
     * @param conn
     * @param sqlStatement
     */
    public StoreDAO(Connection conn, Statement sqlStatement) {
        this.conn = conn;
        this.sqlStatement = sqlStatement;
    }// </editor-fold>

    /**
     *
     * @param storeName
     * @param phone
     * @param taxCode
     * @param aid
     * @return
     */
    protected boolean addStore(String storeName, String phone, String taxCode,
            String aid) {

        try {
            String sqlText =
                    "insert into digistore.clerk " +
                    "(storeName, phone, taxCode,aid) values " + "(" +
                    "'" + storeName + "'," + "'" + phone + "'," + "'" + taxCode +
                    "'," + "'" + aid + "')";

            int value = sqlStatement.executeUpdate(sqlText);
            if (value == 1) {
                conn.commit();
                return true;
            }
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, null, "StoreDAO : regesterStore" + ex);
            //    throw ex;
        }
        return false;
    }

    /**
     *
     * @param storeName
     * @return
     */
    protected boolean deleteStore(String storeName) {
        try {
            String sqlText = "DELETE FROM digistore.store WHERE storeName LIKE" +
                    storeName;
            int value = sqlStatement.executeUpdate(sqlText);
            if (value == 1) {
                conn.commit();
            }
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, null, "StoreDAO : deleteStore" + ex);
        }
        return true;
    }

    /**
     *
     * @param storeName
     * @param phone
     * @param taxCode
     * @param aid
     * @return
     */
    protected boolean updateStore(String storeName, String phone, String taxCode,
            String aid) {
        try {
            String sqlText = "UPDATE digistore.clerk SET phone='" +
                    phone + "', " + "taxCode='" + taxCode + "', " +
                    "aid='" + aid + "' WHERE storeName ='" + storeName + "'";
            int value = sqlStatement.executeUpdate(sqlText);
            if (value == 1) {
                conn.commit();
                return true;
            }
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, null, "StoreDAO : updateStore" + ex);
        }
        return false;
    }
}
